package com.jinmdz.fmis.dao.dao.provider;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 冷藏设备Provider
 *
 * @author LiCongLu
 * @date 2020-02-15 17:54
 */
public class ColdStorageEquipmentProvider {

    /**
     * 按照类型字典编码值查询冷藏设备
     *
     * @param dictCode 类型字典编码
     * @param dataCode 类型编码
     * @return
     * @author LiCongLu
     * @date 2020-02-15 14:48
     */
    public String listColdStorageEquipmentByDictCode(@Param("dictCode") String dictCode, @Param("dataCode") Integer dataCode) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" cold_storage_equipment.id,equipment_type_code,equipment_code,equipment_describe,equipment_state");
                builder.append(" ,column_no,row_no,cold_storage_equipment.disable_state,cold_storage_equipment.sort");
                builder.append(" ,cold_storage_equipment.remark,cold_storage_equipment.version ");
                SELECT(builder.toString());
                FROM(" cold_storage_equipment ");
                LEFT_OUTER_JOIN(" data_dictionary on data_dictionary.data_code = cold_storage_equipment.equipment_type_code  ");
                WHERE(" cold_storage_equipment.deleted = 0 and data_dictionary.deleted = 0 ");
                WHERE(" data_dictionary.dict_code = #{dictCode}");
                WHERE(" data_dictionary.data_code = #{dataCode} ");
                ORDER_BY(" cold_storage_equipment.sort ");
            }
        }.toString();
    }

    /**
     * 按照主键获取冷藏设备信息
     *
     * @param id 主键
     * @return
     * @author LiCongLu
     * @date 2020-02-17 10:52
     */
    public String getColdStorageEquipmentViewById(@Param("id") Integer id) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" cold_storage_equipment.id,equipment_type_code,equipment_code,equipment_describe,equipment_state");
                builder.append(" ,column_no,row_no,cold_storage_equipment.disable_state,cold_storage_equipment.sort");
                builder.append(" ,cold_storage_equipment.remark,cold_storage_equipment.version ");
                SELECT(builder.toString());
                FROM(" cold_storage_equipment ");
                WHERE(" cold_storage_equipment.deleted = 0");
                WHERE(" cold_storage_equipment.id = #{id}");
            }
        }.toString();
    }

    /**
     * 按照类型字典编码值查询冷藏设备
     *
     * @param dictCode 类型字典编码
     * @param dataCode 类型编码
     * @param outState 出藏状态
     * @return
     * @author LiCongLu
     * @date 2020-02-17 10:54
     */
    public String listColdStorageEquipmentViewByDictCode(@Param("dictCode") String dictCode, @Param("dataCode") Integer dataCode, @Param("outState") Integer outState) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" cold_storage_equipment.id,equipment_type_code,equipment_code,equipment_describe,equipment_state");
                builder.append(" ,column_no,row_no,cold_storage_equipment.disable_state,cold_storage_equipment.sort");
                builder.append(" ,cold_storage_equipment.remark,cold_storage_equipment.version");
                builder.append(" ,business_id,appointment_in_time,business_code,process_state_code,dead_name,member_name");

                SELECT(builder.toString());
                FROM(" cold_storage_equipment ");
                LEFT_OUTER_JOIN(" data_dictionary on data_dictionary.data_code = cold_storage_equipment.equipment_type_code  ");

                // 关联占用逝者信息
                StringBuilder businessExt = new StringBuilder();
                businessExt.append(" select cold_storage.id as business_id,appointment_in_time,funeral_business.business_code,process_state_code,equipment_id,dead_name,member_name");
                businessExt.append(" from cold_storage JOIN funeral_business on funeral_business.business_code = cold_storage.business_code ");
                businessExt.append(" where funeral_business.deleted = 0 and cold_storage.deleted = 0 and process_state_code != #{outState} ");

                // 去重
                businessExt.append(" GROUP BY equipment_id ");

                LEFT_OUTER_JOIN(" (" + businessExt.toString() + ") as business on business.equipment_id = cold_storage_equipment.id ");

                WHERE(" cold_storage_equipment.deleted = 0 and data_dictionary.deleted = 0 ");
                WHERE(" data_dictionary.dict_code = #{dictCode}");
                WHERE(" data_dictionary.data_code = #{dataCode} ");
                ORDER_BY(" cold_storage_equipment.sort ");
            }
        }.toString();
    }

}
